<?php

/**
 *
 */
class UploadHelper {

    /**
     * allow user save
     * @param CUploadedFile $uploadFile
     * @return mixed boolean if upload fail
     *
     */
    public static function saveAvatar($uploadFile) {
	//allow extensions?
	if (!in_array($uploadFile->extensionName, array('png', 'jpg', 'jpeg', 'bmp', 'gif'))) {
	    return false;
	}

	$fileName = time() . '.' . $uploadFile->extensionName;
	if ($uploadFile->saveAs(AVATAR_PATH . $fileName)) {
	    //save file successfully
	    return $fileName;
	}

	return false;
    }

    /**
     * upload and save image
     * @param type $uploadFile
     * @param type $folder
     * @return boolean|string
     */
    public static function saveImage($uploadFile, $folder = 'upload') {
	//allow extensions?
	if (!in_array($uploadFile->extensionName, array('png', 'jpg', 'jpeg', 'bmp', 'gif'))) {
	    return false;
	}

	$fileName = $folder . '_' . uniqid() . '.' . $uploadFile->extensionName;
	if ($folder != 'upload') {
	    $uploadPath = UPLOAD_PATH . $folder . DIRECTORY_SEPARATOR;
	} else {
	    $uploadPath = UPLOAD_PATH;
	}
	if ($uploadFile->saveAs($uploadPath . $fileName)) {
	    //save file successfully
	    return $fileName;
	}

	return false;
    }

    /**
     * upload and save image
     * @param type $uploadFile
     * @param type $folder
     * @return boolean|string
     */
    public static function saveDocument($uploadFile, $folder = '') {
	//allow extensions?
	if (!in_array($uploadFile->extensionName, array('png', 'jpg', 'jpeg', 'bmp', 'gif', 'doc', 'docx', 'pdf'))) {
	    return false;
	}

	if ($folder) {
	    $fileName = time() . '.' . $uploadFile->extensionName;
	    if ($uploadFile->saveAs(UPLOAD_PATH . $folder . DIRECTORY_SEPARATOR . $fileName)) {
		//save file successfully
		return $fileName;
	    }
	} else {
	    $fileName = time() . '.' . $uploadFile->extensionName;
	    if ($uploadFile->saveAs(UPLOAD_PATH . 'documents' . DIRECTORY_SEPARATOR . $fileName)) {
		//save file successfully
		return $fileName;
	    }
	}

	return false;
    }

}

